package com.bjsxt.service.impl;

import com.bjsxt.mapper.AdminMapper;
import com.bjsxt.pojo.Admin;
import com.bjsxt.pojo.PageA;
import com.bjsxt.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Insert;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceimpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    //用户登陆验证
    @Override
    public Admin findOne(String aname, String apwd) {
        return adminMapper.selectOne(aname, apwd);
    }

    //查询所有管理员实现
    @Override
    public PageA<Admin> selectAll(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Admin> list = adminMapper.selectAll();

        PageA<Admin> pga = new PageA<>(list, pg.getTotal());

        return pga;
    }

    //添加管理员
    @Override
    public Integer insertAdmin(Admin admin, Integer rid) {
        Integer insert = 0;
        try {
            //添加管理员
            insert = adminMapper.insertAdmin(admin);
            //添加管理员对应的角色
            adminMapper.insertRole(admin.getAid(), rid);
            insert = 1;
        } catch (Exception e) {
            throw e;
        }
        return insert;
    }


}
